package isssr.control.src;


import isssr.control.exception.GoalElicitationException;
import isssr.control.exception.GoalElicitationExceptionCode;
import isssr.dao.exception.DAOException;
import isssr.dao.factory.DAOFactory;
import isssr.dao.factory.DAOFactoryType;
import isssr.dao.interfaces.OrganizationalGoalDAO;
import isssr.entity.exception.IllegalOrganizationalGoalCreationException;
import isssr.entity.exception.IllegalOrganizationalGoalUpdateException;
import isssr.entity.src.OrganizationalGoal;
import isssr.entity.src.OrganizationalGoalDTO;
import isssr.entity.src.User;
import isssr.entity.src.UserContext;
import isssr.entity.src.UserDTO;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;


@ManagedBean(name="organizationalGoalCRUDControl")
@ViewScoped
public class OrganizationalGoalCRUDControl implements Serializable {

	
	private static final long serialVersionUID = 8231358488572348742L;

	@ManagedProperty(value="#{userContext}")
	private UserContext currentContext;
	
		
	public OrganizationalGoalCRUDControl(){}
	

	public void setCurrentContext(UserContext currentContext) {
		this.currentContext = currentContext;
	}

	
	public OrganizationalGoalDTO createGoal() throws GoalElicitationException {
		
		User owner						= this.currentContext.getUser();
		OrganizationalGoalDTO goal_data = null;
		Long sequence_number 			= new Long(0);
		UserDTO user_data				= owner.getDTO();
		String username 				= user_data.getUsername();
		String goalTitle 				= "OrganizationalGoal_"+ username +  "_" + sequence_number;
		DAOFactory factory				= DAOFactory.getDAOFactory(DAOFactoryType.HIBERNATE);
		OrganizationalGoalDAO goalDao 	= factory.getOrganizationalGoalDAO();
		
		try {
				while(goalDao.getByTitle(goalTitle) != null)
				{
					sequence_number++;
					goalTitle = "OrganizationalGoal_"+ username +  "_" + sequence_number;
				}
				
				OrganizationalGoal goal = new OrganizationalGoal(goalTitle, owner);
				goalDao.insert(goal);
				this.currentContext.setCurrent_goal(goal);
				goal_data = goal.getDTO();
				
		} catch (DAOException e) {
			GoalElicitationException ex = new GoalElicitationException(GoalElicitationExceptionCode.UNKNOW_ERROR);
			throw ex;
			
		} catch (IllegalOrganizationalGoalCreationException e) {
			e.printStackTrace();
		}
		
		return goal_data;
	}
	
	
	public OrganizationalGoalDTO readGoal(String title) throws GoalElicitationException {
		DAOFactory factory							= DAOFactory.getDAOFactory(DAOFactoryType.HIBERNATE);
		OrganizationalGoalDAO goalDao 				= factory.getOrganizationalGoalDAO();
		OrganizationalGoal retrieved_goal 			= null;
		OrganizationalGoalDTO retrieved_goal_data 	= null;
		User owner									= this.currentContext.getUser();
		
		try {
			retrieved_goal	= goalDao.getByTitle(title);
			if(retrieved_goal != null)
			{
				retrieved_goal_data = retrieved_goal.getDTO();
				if(owner == null || retrieved_goal_data.getGoalOwner() == null || !owner.equals(retrieved_goal_data.getGoalOwner()))
					retrieved_goal_data = null;
				else
					this.currentContext.setCurrent_goal(retrieved_goal);
			}
		} catch (DAOException e) {
			GoalElicitationException ex = new GoalElicitationException(GoalElicitationExceptionCode.GOAL_NOT_FOUND);
			throw ex;
		}
		
		return retrieved_goal_data;
	}
	
	
	public OrganizationalGoalDTO updateGoal(OrganizationalGoalDTO to_update) throws GoalElicitationException {
		OrganizationalGoalDTO goal_data	= null;
		OrganizationalGoal to_save 		= this.currentContext.getCurrent_goal();
		if(to_save != null)
		{
			System.out.println("current goal not null");
			DAOFactory factory				= DAOFactory.getDAOFactory(DAOFactoryType.HIBERNATE);
			OrganizationalGoalDAO goalDao 	= factory.getOrganizationalGoalDAO();	
			try {
				
				System.out.println(to_save.toString());
				System.out.println(to_update.getFocus());

				to_save.updateOrganizationalGoal(to_update);
				System.out.println(to_save.toString());

				goalDao.update(to_save);
				goal_data = to_save.getDTO();
			} catch (DAOException e) {
				GoalElicitationException ex = new GoalElicitationException(GoalElicitationExceptionCode.GOAL_NOT_FOUND);
				throw ex;
			} catch (IllegalOrganizationalGoalUpdateException e) {
				e.printStackTrace();
			}
		}
		
		return goal_data;
	}
	
	
	public void deleteGoal() throws GoalElicitationException
	{
		OrganizationalGoal to_delete = this.currentContext.getCurrent_goal();
		if(to_delete != null)
		{
			DAOFactory factory				= DAOFactory.getDAOFactory(DAOFactoryType.HIBERNATE);
			OrganizationalGoalDAO goalDao 	= factory.getOrganizationalGoalDAO();
			try {
				goalDao.delete(to_delete);
				this.currentContext.setCurrent_goal(null);
			} catch (DAOException e) {
				GoalElicitationException ex = new GoalElicitationException(GoalElicitationExceptionCode.GOAL_NOT_FOUND);
				throw ex;
			}
		}
	}
	
}
